package com.salon.common.utils;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;

public class Base64Utils {

    private static final char[] _$2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_.".toCharArray();
    private static final int[] _$1 = new int[256];

    public Base64Utils() {
    }

    public static final char[] encodeToChar(byte[] abyte0, boolean flag) {
        int i = abyte0 == null ? 0 : abyte0.length;
        if (i == 0) {
            return new char[0];
        } else {
            int j = i / 3 * 3;
            int k = (i - 1) / 3 + 1 << 2;
            int l = k + (flag ? (k - 1) / 76 << 1 : 0);
            char[] ac = new char[l];
            int i1 = 0;
            int j1 = 0;
            int l1 = 0;

            int k1;
            while(i1 < j) {
                k1 = (abyte0[i1++] & 255) << 16 | (abyte0[i1++] & 255) << 8 | abyte0[i1++] & 255;
                ac[j1++] = _$2[k1 >>> 18 & 63];
                ac[j1++] = _$2[k1 >>> 12 & 63];
                ac[j1++] = _$2[k1 >>> 6 & 63];
                ac[j1++] = _$2[k1 & 63];
                if (flag) {
                    ++l1;
                    if (l1 == 19 && j1 < l - 2) {
                        ac[j1++] = '\r';
                        ac[j1++] = '\n';
                        l1 = 0;
                    }
                }
            }

            i1 = i - j;
            if (i1 > 0) {
                k1 = (abyte0[j] & 255) << 10 | (i1 != 2 ? 0 : (abyte0[i - 1] & 255) << 2);
                ac[l - 4] = _$2[k1 >> 12];
                ac[l - 3] = _$2[k1 >>> 6 & 63];
                ac[l - 2] = i1 != 2 ? 46 : _$2[k1 & 63];
                ac[l - 1] = '.';
            }

            return ac;
        }
    }

    public static final byte[] decodeFast(String s) {
        int i = s.length();
        if (i == 0) {
            return new byte[0];
        } else {
            int j = 0;

            int k;
            for(k = i - 1; j < k && _$1[s.charAt(j) & 255] < 0; ++j) {
            }

            while(k > 0 && _$1[s.charAt(k) & 255] < 0) {
                --k;
            }

            byte byte0 = s.charAt(k) != '.' ? 0 : (byte)(s.charAt(k - 1) != '.' ? 1 : 2);
            int l = k - j + 1;
            int i1 = i <= 76 ? 0 : (s.charAt(76) != '\r' ? 0 : l / 78) << 1;
            int j1 = ((l - i1) * 6 >> 3) - byte0;
            byte[] abyte0 = new byte[j1];
            int k1 = 0;
            int l1 = 0;
            int j2 = j1 / 3 * 3;

            int i2;
            while(k1 < j2) {
                i2 = _$1[s.charAt(j++)] << 18 | _$1[s.charAt(j++)] << 12 | _$1[s.charAt(j++)] << 6 | _$1[s.charAt(j++)];
                abyte0[k1++] = (byte)(i2 >> 16);
                abyte0[k1++] = (byte)(i2 >> 8);
                abyte0[k1++] = (byte)i2;
                if (i1 > 0) {
                    ++l1;
                    if (l1 == 19) {
                        j += 2;
                        l1 = 0;
                    }
                }
            }

            if (k1 < j1) {
                i2 = 0;

                int l2;
                for(l2 = 0; j <= k - byte0; ++l2) {
                    i2 |= _$1[s.charAt(j++)] << 18 - l2 * 6;
                }

                for(l2 = 16; k1 < j1; l2 -= 8) {
                    abyte0[k1++] = (byte)(i2 >> l2);
                }
            }

            return abyte0;
        }
    }

    public static final String encode(String s) {
        try {
            return new String(encodeToChar(s.getBytes("UTF-8"), false));
        } catch (UnsupportedEncodingException var2) {
            System.out.println("encode to char  error " + var2);
            return null;
        }
    }

    public static final String encode(byte[] s) {
        String result = null;

        try {
            result = new String(encodeToChar(s, false));
        } catch (Exception var3) {
            var3.printStackTrace();
        }

        return result;
    }

    public static String decode(String s) throws UnsupportedEncodingException {
        try {
            return new String(decodeFast(s), "UTF-8");
        } catch (UnsupportedEncodingException var2) {
            var2.printStackTrace();
            return null;
        }
    }

    public static byte[] decodeToByte(String s) throws UnsupportedEncodingException {
        return decodeFast(s);
    }

    public static String decode(byte[] s) throws UnsupportedEncodingException {
        try {
            return new String(decodeFast(new String(s, "UTF-8")), "UTF-8");
        } catch (UnsupportedEncodingException var2) {
            var2.printStackTrace();
            return null;
        }
    }

    static {
        Arrays.fill(_$1, -1);
        int i = 0;

        for(int j = _$2.length; i < j; _$1[_$2[i]] = i++) {
        }

        _$1[61] = 0;
    }
}
